<?php

class Application_Model_DbTable_TaobaoItems extends Zend_Db_Table_Abstract
{

    protected $_name = 'txy_taobao_items';
	
    public function addNewTaoBaoItems($data){
        try{
            $this->initRelItems($data['num_iid']);
        	$this->insert($data);
        }catch (Exception $e){
            throw new Exception("新添加商品异常,请检查商品表及关联商品表");
        }
    }
    
    public function isRepeat($num_iid){
    	$res=$this->fetchRow("num_iid='".$num_iid."'");
    	if($res){
    		return false;
    	}
    	return true;
    }
    /**
     * Enter description here ...
     * @param unknown_type $index 用于确定show_level的查询条件
     * @param unknown_type $limit
     */
    public function getHotItems($index=2,$limit=8){
    	$select=new Zend_Db_Table_Select($this);
    	$str='is_taobaoke=1 and is_onsale<119';
    	if(0==$index){
    		$str=$str.' and show_level=0';
    	}
    	if(1==$index){
    		$str=$str.' and show_level=1';
    	}
    	$select->from(array('t'=>'txy_taobao_items'),array('t.num_iid','t.title','t.item_url_spell','t.price','t.is_promotion','t.promotion_price','t.promotion_name','t.pic_url','t.freight_payer'))
    		   ->where($str)->order('count_num desc')->limit($limit);
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    
    public function getPromotionItems($index=2,$limit=8){
    	$select=new Zend_Db_Table_Select($this);
    	$str='is_promotion=1 and is_onsale<119';
    	if(0==$index){
    		$str=$str.' and show_level=0';
    	}
    	if(1==$index){
    		$str=$str.' and show_level=1';
    	}
    	$select->from(array('t'=>'txy_taobao_items'),array('t.num_iid','t.title','t.item_url_spell','t.price','t.is_promotion','t.promotion_price','t.promotion_name','t.pic_url','t.freight_payer'))
    		   ->where($str)->order('update_time desc')->limit($limit);
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    
	public function getNewItems($index=2,$limit=8){
    	$select=new Zend_Db_Table_Select($this);
    	$str='is_taobaoke=1 and is_onsale<119';
    	if(0==$index){
    		$str=$str.' and show_level=0';
    	}
    	if(1==$index){
    		$str=$str.' and show_level=1';
    	}
    	$select->from(array('t'=>'txy_taobao_items'),array('t.num_iid','t.title','t.item_url_spell','t.price','t.is_promotion','t.promotion_price','t.promotion_name','t.pic_url','t.freight_payer'))
    		   ->where($str)->order('add_time desc')->limit($limit);
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    
    
    /**
     * 这个方法以后要改
     * Enter description here ...
     * @param unknown_type $limit
     */
    public function getSidebarHotItems($limit=8,$numiid=""){
    	$select=new Zend_Db_Table_Select($this);
    	$select = $this->select()->setIntegrityCheck(false);
    	if(""!=$numiid){
    	    $select->from(array('t'=>'txy_taobao_items'),array('t.num_iid','t.title','t.price','t.is_promotion','t.promotion_name','t.promotion_price','t.item_url_spell','t.pic_url'))
    		   ->where('b.main_numiid='.$numiid)->joinInner(array('b'=>'txy_item_priority'),'t.num_iid=b.rel_numiid')->order('b.rel_priority desc')->limit($limit);
        	$res=$this->fetchAll($select);
        	if(count($res)>0){
        	    return $res->toArray();
        	}
    	}
    	$select=new Zend_Db_Table_Select($this);
    	$select->from(array('m'=>'txy_taobao_items'),array('m.num_iid','m.title','m.price','m.is_promotion','m.promotion_name','m.promotion_price','m.item_url_spell','m.pic_url'))
    		   ->where('m.is_taobaoke=1')->order('rand()')->limit($limit);
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    /**
     * 替代目前的getSidebarHotItems方法
     * Enter description here ...
     * @param unknown_type $limit
     */
    public function getRandItems($limit=10){
        $select=new Zend_Db_Table_Select($this);
    	$select->from(array('t'=>'txy_taobao_items'),array('t.num_iid','t.title','t.price','t.is_promotion','t.promotion_name','t.promotion_price','t.item_url_spell','t.pic_url'))
    		   ->where('is_taobaoke=1 and is_onsale=1')->order('rand()')->limit($limit);
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    
    public function getItemsByNumiid($num_iid){
    	$res=$this->fetchRow("num_iid='".$num_iid."'");
    	if($res){
    		return $res->toArray();
    	}
    }
    
	public function updateCount($numiid,$count){
    	$data=array('count_num'=>$count);
    	$this->update($data, 'num_iid='.$numiid);
    }
    
    public function getNeedUpdatePromotionItems($limit=10){
    	$select=new Zend_Db_Table_Select($this);
    	$select->from(array('t'=>'txy_taobao_items'),array('t.num_iid'))
    		   ->where('is_onsale<118 and (is_promotion=2 or(is_promotion=0 and update_time< DATE_SUB( now() , INTERVAL 1 DAY )) or (is_promotion=1 and promotion_end_time < now()))')
    		   ->limit($limit);
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    
 	public function getItemsAndStatus(){
    	$select=new Zend_Db_Table_Select($this);
    	$select->from(array('t'=>'txy_taobao_items'),array('t.num_iid','t.is_taobaoke','t.is_onsale'));
    		   //->where('is_promotion!=1');
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    
	public function updateTaobaoItems($data,$numiid){
		$this->update($data, "num_iid='".$numiid."'");
	}
	
	public function getRelItems($sql){
		$res=$this->fetchAll('num_iid in('.$sql.')');
		return $res;
	}
	/**
	 * 随机初始化关联的商品
	 * Enter description here ...
	 * @param unknown_type $numiid
	 */
	public function initRelItems($numiid){
	    $res=$this->getRandItems();
	    $dbItemPriority=new Application_Model_DbTable_ItemPriority();
	    $data=array();
	    $data['main_numiid']=$numiid;
	    foreach($res as $re){
	        $data['rel_numiid']=$re['num_iid'];
	        $data['rel_priority']=0;
	        $dbItemPriority->addNewPriority($data);
	    }
	}
    

}

